<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <canvas id="oCanvas" width="500" height="400"></canvas>
    <script src="../../cuon-matrix.js"></script>
    <script type="notjs" id="vertex">
        // 存储限定符    数据类型   变量名称
        attribute vec4 a_position;
        attribute vec4 a_color;
        varying vec4 f_color;
        uniform mat4 u_viewMatrix;
        void main() {
            gl_Position = u_viewMatrix * a_position;
            f_color = a_color;
        }
    </script>
    <script type="notjs" id="fragment">
        precision mediump float;
        varying vec4 f_color;
        void main() {
            gl_FragColor = f_color;
        }
    </script>
    <script>
        var oCanvas = document.getElementById('oCanvas');
        var gl = oCanvas.getContext('webgl');

        if (!gl) {
            alert('浏览器不支持webgl');
        }

        function  createShader(gl, type, source) {
            // 创建着色器
            var shader = gl.createShader(type);
            // 
            gl.shaderSource(shader, source);
            // 将着色器数据源转化成代码
            gl.compileShader(shader);

            // 判断着色器是否建立完成  
            var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);

            if (success) {
                return shader;
            }
            console.log(gl.getShaderInfoLog(shader));
        }
        // gl.VERTEX_SHADER    gl.FRAGMENT_SHADER

        var vertexStr = document.getElementById('vertex').innerText;
        var fragmentStr = document.getElementById('fragment').innerText;

        // 创建着色器
        var vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexStr);
        var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentStr);

        function createProgram(gl, vertexShader, fragmentShader) {
            var program = gl.createProgram();
            gl.attachShader(program, vertexShader);
            gl.attachShader(program, fragmentShader);
            gl.linkProgram(program);

            var success = gl.getProgramParameter(program, gl.LINK_STATUS);
            if (success) {
                return program;
            }
            console.log(gl.getProgramInfoLog(program));
        }

        var program = createProgram(gl, vertexShader, fragmentShader);
        gl.useProgram(program);

        var a_position = gl.getAttribLocation(program, "a_position");
        var a_color = gl.getAttribLocation(program, "a_color");
        var u_viewMatrix = gl.getUniformLocation(program, "u_viewMatrix");
        console.log(a_position);
        // gl.vertexAttrib3f(a_position, 0, 0, 0);
        // var pointSizeBuffer = gl.createBuffer();
        // gl.bindBuffer(gl.ARRAY_BUFFER, pointSizeBuffer);
        // var sizeData = new Float32Array([
        //     10.0, 20.0, 30.0
        // ]);
        // gl.bufferData(gl.ARRAY_BUFFER, sizeData, gl.STATIC_DRAW);
        // gl.vertexAttribPointer(a_pointsize, 1, gl.FLOAT, false, 0, 0);
        // gl.enableVertexAttribArray(a_pointsize);

        var viewMatrix = new Matrix4();
        viewMatrix.setLookAt(0, 0, 0.5, 0, 0, 0, 0, 1, 0 );
        gl.uniformMatrix4fv(u_viewMatrix, false, viewMatrix.elements);
        var vertexBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
        var positionData = new Float32Array([
            0, 0.5, -0.4, 1, 0, 0,
            -0.5, -0.5, -0.4, 1, 0, 0,
             0.5, -0.5, -0.4, 1, 0, 0,


             0, 0.5, -0.2, 0, 1, 0,
             -0.5, -0.5, -0.2, 0, 1, 0,
             0.5, -0.5, -0.2, 0, 1, 0,

            0, 0.5, 0, 0, 0, 1,
            -0.5, -0.5, 0, 0, 0, 1,
            0.5, -0.5, 0, 0, 0, 1,

        ]);
        gl.bufferData(gl.ARRAY_BUFFER, positionData, gl.STATIC_DRAW);
        // stride :  两个顶点间的字节数
        // offset: 数据的偏移量
        gl.vertexAttribPointer(a_position, 3, gl.FLOAT, false, 4 * 6, 0 * 4 );
        gl.vertexAttribPointer(a_color, 3, gl.FLOAT, false, 4 * 6, 4 * 3);
        gl.enableVertexAttribArray(a_position);
        gl.enableVertexAttribArray(a_color);
     


        gl.drawArrays(gl.TRIANGLES, 0, 9);



    
    </script>
</body>
</html>
